home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 43 / Mac Magazin and MacEasy Magazine CD - Issue 43.iso / Software / Mobiles Büro / Newton / Newton Utilities / NewtDB 1.3 ƒ / Newton Script Spport / NS code Examples.txt < prev   
Text File  |  1997-11-28  |  13KB  |  297 lines

  1. Newton Script Examples
  2. ----------------------
  3.  
  4.  
  5. Here are the most asked for examples. They realy do work as they have been typed into a MP2k and executed.
  6.  
  7.  
  8. Example #1 - Put the date into a field so that later it can be used in calculations or sorted.
  9.  
  10. "Run Script always" is checked.
  11.  
  12. If RecordAction = 'NewMain then begin   // only execute this part if a new record is made.
  13.      local nTime := time();                            // get the time in minutes
  14.      local TTime := :ToString(nTime);                  // make time into text
  15.      local DbRecord := :GetEntry(CurrentDbCursor);     // get the current record being displayed
  16.      :SetField(CurrentDb, DbRecord, 8, TTime);         // set the time (as text) into field #8
  17.      :SaveEntry(DbRecord);                            // save the record
  18. end;
  19.  
  20. local rTime := :GetField(CurrentDbEntry,8);        // get field #8 in the current record on screen
  21.      :Show(:FormDateString(:toNumber(rTime)));     // show the formatted date on the screen, first changing the date to a number
  22.  
  23.  
  24.  
  25.  
  26. The above sets the current time into a field (#8) in the current screens database when a New record is made. It shows that date formatted (YY/MM/DD or whatever you have the Date format set to) on the screen where the NS button is. You don't put field #8 on the screen, if you do , it will show up as a number.
  27.  
  28. It also demonstrates two ways to access the current record on the screen. First by using the var: CurrentDbCursor  and then by using the var: CurrentDbEntry.
  29.  
  30.  
  31. -----------------
  32. Example #2: Display the Date Calendar to pick a date.
  33.  
  34. // Do NOT delete the following line or else there might be a memory leak.
  35. // There is a MP2K ROM problem with the postkeyString()
  36. // function if there is no receiver of the text.
  37.  
  38.  
  39. self.pickActionScript := func(sd)    // this is callback, it is not directly executed
  40. begin
  41.    :Show(:formdatestring(sd[0]));        // show the date picked in the NS button
  42.  
  43. // use the below 2 lines to put the date as a number into an edit field
  44.    if DataformClick = 0 then return;   // no edit field is open
  45.    postkeyString('viewFrontKey,:toString(sd[0]));
  46.  
  47. // use the 2 lines below to put the date as formatted (YY-MM-DD or whatever your prefs are set to) into the edit field.
  48.   if DataformClick = 0 then return;   // no edit field is open
  49.   postkeyString('viewFrontKey,:FormDateString(sd[0]));
  50.  
  51.  
  52. // use the below 3 lines to put the date (as a text number) into a database field
  53.      :SetField(CurrentDbEntry, DbRecord, 8, :toString(sd[0]));      // set the time (as text) into field #8 of the on screen Db
  54.      :SaveEntry(DbRecord);                                     // save the record
  55.  
  56.  
  57.    inherited:?pickActionScript (sd);     // call the internal script
  58.    NsVar.a:=nil;                         // remove (most) storage
  59. end;
  60.  
  61.  
  62. self.pickCancelledScript := func()      // this is callback, it is not directly executed
  63. begin
  64.    inherited:?pickCancelledScript();    // call the internal script
  65.    NsVar.a:=nil;                        // remove (most) storage
  66. end;
  67.  
  68. local p := @317;                        // internal magic pointer for a Date Popup
  69. NsVar.a :=buildContext(p);              // make it into a view and save a reference to it
  70.  
  71.   // this bounds frame can be used to make the date popup show on the left side of the screen
  72. //local w:= {top:5,left:5,bottom:6,right:6};
  73.  
  74. // or the bounds frame below will show the date popup on the left side
  75. local w :=getroot():globalbox();
  76. NsVar.a:new([time()],w, self);        // shows the date popup, when used taps [x] box, then PickActionScript is executed
  77.  
  78.  
  79. The above code will pop up a Date calendar for selection of a Date. Three options are given as what to do with the date a user selects.
  80.  
  81.  
  82. ----------
  83. Example #3 - Calculate a total of a field. Set the script to always run.
  84.  
  85. local t :=0;                                 // the var that holds tht total
  86. if NSvar.PartTotal and not ButtonHit  then   // check if a total exists
  87. begin
  88.      t := NSvar.PartTotal;                   // total already exists , so use it
  89. end;
  90.  
  91. else begin
  92.  
  93.    :Show("Calculating ...");                 // a total doesn't exist, so calculate
  94.  
  95.    local c :=:GetCursor(7, 0);               // get a cursor for Db #7 using index #0
  96.    If NsError then begin
  97.       :ShowError("Opps, the database/index doesn't exist");
  98.       return;
  99.    end;
  100.    local e:= :GetEntry(c);                   // get the first record from the Db
  101.  
  102.    while e do                                // loop while there are more records
  103.    begin
  104.      local nt :=:GetField(e, 2);             // get data from field #2
  105.      If NsError then 
  106.         :Show("Opps, the Field doesn't exist in that record");
  107.  
  108.      local n := :ToNumber(nt);                // convert to number
  109.      If NsError then 
  110.         :Show("Opps, a field didn't contain a number");
  111.       else
  112.         t:=t+n;                                  // add to total
  113.      e:=:NextEntry(c)                        // get next record from database
  114.    end;
  115. end;
  116.  
  117. :Show("Total: " & :ToString(t));           // convert total back to text and show on the screen
  118.  NSvar.PartTotal := t;                      // put the number in the NS save frame
  119. :SaveNSvar();                               // save the vars for next time
  120.  
  121. You may want to add some more initial conditions to recalculate the total based on a changed field, added or deleted record.
  122. ----------
  123.  
  124.  
  125.  
  126.  
  127. Example #4 - Print a multi column report
  128.  
  129.  
  130. // this prints a multi column report
  131. local MyTabs :=  [100,200];                // tabs are in pixels, held in an array. you may set up to 8 tab stops
  132. setvalue(PrintPage.ppt, 'tabs, MyTabs);     // set the tabs
  133.  
  134. local DbCursor := CurrentDbCursor;        // cursor for screen db including any Query that is active
  135. :ResetCursor(DbCursor);                   // set cursor to start of database
  136. local Page := 0;                          // init page number
  137. Local MainTitle := "\tSample Report";              // the report title, at 1st tab stop
  138. local ColumnTitle := "Item\tCost\tPart Number";    // the column titles, at the tab stops
  139.  
  140. local DbEntry := :GetEntry(DbCursor);              // get 1st record
  141. local T := MainTitle & "\n" & ColumnTitle ;        // report text goes into 'T', start with the heading
  142. local Lines := 0;                                  // count lines per page
  143. :show("Printing page 1");                          // update stats
  144.  
  145. While  DbEntry do begin                  // loop for all records
  146.  
  147. // get the data to be used
  148.  
  149.   local t0 := :GetField(DbEntry, 0);     // field #0 is 1st column
  150.   local t1 := :GetField(DbEntry, 1);     // field #1 is 2nd column
  151.   local t2 := :GetField(DbEntry, 2);     // field #2 is 3rd column
  152.  
  153. // if more than 3 fields, then add here, like:
  154. //   local t3 := :GetField(DbEntry, 3);     // field #3 is 4th column
  155. // and add: ' & "\t" & t3 ' (no single quotes)     to the line below so that data is appended to the other data
  156.  
  157.   T := T & "\n" & t0 & "\t" & t1 & "\t" & t2;   // put all data together, separated with tabs
  158.  
  159.   Lines := lines +1;                     // increment line counter
  160.   if lines > 50 then begin               // when we hit 50 lines, print the page to the notepad
  161.      Page := page +1;                    // increment page counter
  162.      :show("Printing page"&:toString(page));  // update status
  163.      Lines := 0;                              // reset line counter
  164.      T := T & "\n\tPage: "&:ToString(page);   // add page number to page of text
  165.      :MakeNote(T);                            // put all the report text to a note in the notepad
  166.      T := MainTitle & "\n" & ColumnTitle ;          // init report heading for next page
  167.   end;
  168.  
  169.   if NsError then begin                       // if there was any error, give up
  170.     :show("Error: " & :ToString(NsError));
  171.     return;
  172.   end;
  173.   DbEntry := :NextEntry(DbCursor);           // get next record
  174.  
  175. end;
  176.  
  177. if Lines > 0 then begin               // after last record, print last page
  178.    T := T & "\n\tPage: "&:ToString(page+1);
  179.    :MakeNote(T);
  180. end;
  181. :Show("Print Done");
  182.  
  183.  
  184. Notes:
  185.  
  186. Set the MyTabs array to as many (up to 8) tab stops as needed for up to 9 columns of data.
  187.  
  188. Change the number of lines per page (NotePad note) by changing the "if lines > 50 then begin " to another number, but remember that a Note should be less than 8k is size.
  189.  
  190. In the NotePad, you can switch to OverView and select multiple items to print/fax/email as one job.
  191.  
  192.  
  193. -----------
  194.  
  195.  
  196. Example #5 - Make a FileMaker type Portal showing multiple fields of multiple records
  197.  
  198. NsDontClick:=true;                       // set NS window so it can't be clicked
  199. local myTabs := [100,200];               // there will be 3 coulmns so set 2 (max: 8) tab stops. Tabs are in pixels
  200. SetValue(self, 'tabs, MyTabs);           // set the tabs to the button view
  201.  
  202. DbNumber := 6;                          // the database to get the records from
  203. MatchFieldNumber := 0;                  // the field to find a match in. Use an Indexed field for fast lookups and then the data fetched will be in order. Non-indexed searches are SLOW  and out of order.
  204.     
  205. MatchDataStr := "d";                    // the match data. As many or few chars as you want. This could be data from a record.
  206.  
  207. local DbEntryCursor :=  :GetDbIndirectRecord(DbNumber, MatchFieldNumber, MatchDataStr);  // get the 1st record that matches 'MatchDataStr'
  208.  
  209. local DbEntry:= DbEntryCursor.DbEntry;   // get the matched record entry
  210. local DbCursor:= DbEntryCursor.DbCursor; // get the cursor that points to the matched record
  211.  
  212. If not DbEntry                           // check to see if there was indeed a match
  213.    then begin
  214.      :Show("No Records to display");     // if no matched record, then exit
  215.       return;
  216. end;
  217.  
  218. local  max := 9;                         // there was a matched record, so now fetch nine more
  219. local t := "Part\tNumber\tCost";         // define the title
  220.  
  221. if DbCursor  then                        // if there is a cursor then start the loop. May be not needed
  222. while DbEntry do                         // loop while getting records
  223. begin
  224.  
  225. // form the data to be displayed by putting a <cr> at the start of each line
  226. // and a tab (\t) before the 2nd and 3rd data columns.
  227.  
  228. t := t & "\n"& :GetField(DbEntry, 0) & "\t" & :GetField(DbEntry, 1) & "\t" & :GetField(DbEntry, 2);
  229. max := max - 1;                         // decrement max records to fetch counter
  230. if max < 0 then break;                  // break out of 'while loop' if all fetched
  231. DbEntry := :NextEntry(DbCursor);        // get the next record
  232.  
  233. end;                                    // end of loop
  234.  
  235. :Show(t);                               // show all fetched records on screen. 
  236.  
  237.  
  238. ----------
  239.  
  240. Example #6 - Math
  241.  
  242. local n1 := :ToNumber(:GetField(CurrentDbEntry, 1));    // get data from field #1 of on screen Db
  243. local n2 := :ToNumber(:GetField(CurrentDbEntry, 2));    // get data from field #2 of on screen Db
  244.  
  245. local t := n1 + n2;                                     // add field #1 and field #2, save in 't'
  246.  
  247. :show("Total =" & :ToString(t));                        // display the result
  248.  
  249.  
  250. ----------
  251.  
  252.  
  253. Example #7 - Popup List
  254.  
  255.  
  256. local aList := ["A","B","C"];                                                                                    // define an array of the items to popup
  257. addArraySlot(aList, 'pickseparator);              // add a separator line in the list
  258. addArraySlot(aList, "D");                         // add some more data to the list
  259.  
  260. // pop up the list on the screen.
  261. // The 2nd and 3rd parameters of 'dopopup' are offsets (X,Y) 
  262. // that indicate how far from the button you want the list to appear on the screen.
  263.  
  264. dopopup(aList,0,0,self);
  265.  
  266.  
  267.  
  268. // this is a callback routine that gets executed only when an item in the popup list is selected
  269.  
  270. self.pickActionScript :=
  271. func(selected)
  272. begin
  273.  
  274. // show which item was selected
  275. // items start at 0. Even pickseparator's take space but  you can't select them, thus it's
  276. // number will never be returned
  277.  
  278. :show("item# "& :toString(selected) & " : " & aList[ selected ]);
  279. end;
  280.  
  281.  
  282.  
  283. // this is a callback routine that gets executed only when no item in the list is selected
  284. // if you don't want to do anything when no item is selected, this doesn't need to be entered.
  285.  
  286. self.pickCancelledScript :=
  287. func()
  288. begin
  289. :show("nothing");
  290. end;
  291.  
  292.  
  293. Notes: You can make a popup containing dynamic data, perhaps a list of data of a field from all records in a database by using the addArraySlot function while retrieving the field data in a loop and then, when the user selects an item in the popup list, putting the selection data into a record. See other examples on how to retrieve the record data & putting data into a record.
  294. ----------
  295.  
  296.  
  297.